package com.vecspace.msoffice;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.vecspace.exception.HException;

public class CellTable {
	private String tableName;
	private int columnCount;
	private CellColumn[] columns;
	private int[] mergeColumns;//add by yuehan
	
	private List<CellRow> rows = new ArrayList<>(512);
	
	public CellTable(int columnCount) {
		this.columnCount = columnCount;
		this.columns = new CellColumn[columnCount];
		for(int i=0; i<this.columns.length; ++i) {
			if(this.columns[i] == null) {
				this.columns[i] = new CellColumn();
			}
		}
	}
	
	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public CellRow getRow(int index) {
		return rows.get(index);
	}
	
	public int getRowCount() {
		return rows.size();
	}
		
	public int getColumnCount() {
		return columnCount;
	}
	
	public CellColumn getColumn(int columnIndex) {
		return this.columns[columnIndex];
	}
	
	/**
	 * 需要合并的列号
	 * @return 
	 * @reviewer: 
	 */

	public int[] getMergeColumns() {
		return mergeColumns;
	}

	public void setMergeColumns(int[] mergeColumns) {
		this.mergeColumns = mergeColumns;
	}

	/**返回一个新行，但是不加入到table中
	 * 
	 * @return 
	 * @author: wanghua
	 * @reviewer: 
	 */
	public CellRow newRow() {
		return new CellRow(columnCount);
	}
	
	public void appendRow(CellRow row) {
		rows.add(row);
	}
	
	/**Inserts the specified element at the specified position in this list (optional operation). Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).
	 * 
	 * @param index
	 * @param row 
	 * @author: wanghua
	 */
	public void insertRow(int index, CellRow  row) {
		rows.add(index, row);
	}
	
	public CellRow removeRow(int index) {
		return rows.remove(index);
	}
	
	public void appendColumns(int newColumnCount) throws HException {
		this.columnCount += newColumnCount;
		CellColumn[] columns2 = new CellColumn[columnCount];
		System.arraycopy(columns , 0, columns2, 0, columns.length);
		this.columns = columns2;
		for(int i=0; i<this.columns.length; ++i) {
			if(this.columns[i] == null) {
				this.columns[i] = new CellColumn();
			}
		}
		
		for(CellRow row : rows) {
			row.appendColumns(newColumnCount);
		}
	}
	
	private CellValue getCell(int rowIndex, int columnIndex) {
		CellRow cellRow = this.getRow(rowIndex);
		return cellRow.getValue(columnIndex);
	}
	
	private String getCellText(int rowIndex, int columnIndex) {
		CellRow cellRow = this.getRow(rowIndex);
		String text = cellRow.getText(columnIndex);
		return text;
	}
	
	public void mergeCellsOfCoumns() {
		//change by yuehan
		if(mergeColumns != null && mergeColumns.length > 0) {//如果预设了需要合并的列，则仅执行合并对应列否则全部执行
			for(int i=mergeColumns.length - 1; i>=0; --i) {
				if(mergeColumns[i] <= this.columnCount-1) {
					mergeCellsOfCoumn(mergeColumns[i]);
				}
			}
		}else {
			for(int i=this.columnCount-1; i>=0; --i) {
				mergeCellsOfCoumn(i);
			}
		}
	}
	
	private void mergeCellsOfCoumn(int columnIndex) {
		int rowCount = rows.size();
		if(rowCount < 2)return;
		
		int mergeStartRowIndex = 0;
		while(mergeStartRowIndex < rowCount) {
			String preRowCellText = this.getCellText(mergeStartRowIndex, columnIndex);
			if(preRowCellText == null || preRowCellText.trim().length() == 0) {
				++mergeStartRowIndex;
				continue;
			}
			preRowCellText = preRowCellText.trim();
			String preColCellText = "";
			if(columnIndex > 0) {
				preColCellText = this.getCellText(mergeStartRowIndex, columnIndex - 1);
				if(preColCellText == null) preColCellText = "";
			}
			int rowIndex = mergeStartRowIndex + 1; 
			for(; rowIndex < rowCount; ++rowIndex) {
				String currText = this.getCellText(rowIndex, columnIndex);
				if(currText != null) currText = currText.trim();
				String currPreColCellText = "";
				if(columnIndex > 0) {
					currPreColCellText = this.getCellText(rowIndex, columnIndex - 1);
					if(currPreColCellText == null) currPreColCellText = "";
				}
				if(!StringUtils.equals(preColCellText, currPreColCellText) || !StringUtils.equals(preRowCellText, currText)) {
					break;
				}
			}
			if(rowIndex > mergeStartRowIndex + 1) {
				int endRowIndex = rowIndex - 1;//rowIndex 已经不属于合并内容了,所以需要-1
				CellValue firstCell = this.getCell(mergeStartRowIndex, columnIndex);
				firstCell.setMergedCell(true);
				firstCell.setMergeReginFirsetCell(true);
				firstCell.setRowSpan(endRowIndex - mergeStartRowIndex + 1);
				for(int rowIndex2 = mergeStartRowIndex + 1; rowIndex2 < rowIndex; ++ rowIndex2) {
					CellValue mergedCell = this.getCell(rowIndex2, columnIndex);
					mergedCell.setMergedCell(true);
					mergedCell.setMergeReginFirsetCell(false);
				}
			}
			mergeStartRowIndex = rowIndex;
		}
		
	}
}
